<template>
  <el-dialog
    :title="showTitle"
    :visible.sync="visible"
    :before-close="handleClose"
  >
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item label="部门名称" :label-width="formLabelWidth" prop="name">
        <el-input v-model="form.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="部门编码" :label-width="formLabelWidth" prop="code">
        <el-input v-model="form.code" autocomplete="off" />
      </el-form-item>
      <el-form-item label="部门负责人" :label-width="formLabelWidth">
        <el-select v-model="form.manager" placeholder="请选择负责人" @focus="getEmployeeSimple">
          <el-option
            v-for="itme in people"
            :key="itme.id"
            :label="itme.username"
            :value="itme.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        label="部门介绍"
        :label-width="formLabelWidth"
        prop="introduce"
      >
        <el-input v-model="form.introduce" autocomplete="off" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button
        type="primary"
        @click="onsubmit"
      >确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDepartments, addDepartments, getDepartDetail, upDateDepartments } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  name: 'VueAdminTemplateMaster2AddTools',
  props: {
    visible: Boolean,
    treeNode: Object
  },
  data() {
    const validateName = async(rule, value, callback) => {
      const { depts } = await getDepartments()
      console.log(depts)
      let arr = []
      if (this.form.id) {
        arr = depts.filter(itme => itme.id !== this.treeNode.id && itme.pid === this.treeNode.pid)
      } else {
        arr = depts.filter(itme => itme.id === this.treeNode.id)
      }
      console.log(arr)
      const isRepat = arr.some(itme => itme.name === value)
      isRepat ? callback(new Error(`该部门下已存在${value}部门`)) : callback()
    }
    const validateCode = async(rule, value, callback) => {
      const { depts } = await getDepartments()
      console.log(depts)
      const repeat = depts.some(itme => itme.code === value && value)
      repeat ? callback(new Error(`组织架构中已经有部门使用${value}编码`)) : callback()
    }
    return {
      // dialogFormVisible: false,
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      // 定义校验规则
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 10, message: '部门名称要求1-10个字符', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 10, message: '部门编码要求1-10个字符', trigger: 'blur' },
          { validator: validateCode, trigger: 'blur' }
        ],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-300个字符' }
        ]
      },
      formLabelWidth: '120px',
      people: []
    }
  },
  computed: {
    showTitle() {
      return this.form.id ? '编辑部门' : '新增子部门'
    }
  },
  methods: {
    // 获取员工列表
    async getEmployeeSimple() {
      this.people = await getEmployeeSimple()
    },
    handleClose(done) {
      this.formData = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$refs.form.resetFields()
      this.$emit('update:visible', false)
    },
    async onsubmit() {
      await this.$refs.form.validate()
      if (this.form.id) {
        await upDateDepartments(this.formData)
        this.$message.success('编辑成功')
      } else {
        await addDepartments({ ...this.form, pid: this.treeNode.id })
        this.$message.success('新增成功')
      }

      this.$emit('addDepts')
      this.$emit('update:visible', false)
    },
    async  getDepartDetail(id) {
      // 请求来的数据回显给表单
      this.form = await getDepartDetail(id)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
